package controller;

import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import model.entity.ReinsuranceBillingEntity;
import baseclass.BaseCreateBill;
import baseclass.ErrorPage;

public class CreateBill extends BaseCreateBill<ReinsuranceBillingEntity>
{
    public CreateBill() throws IOException, WriteException
    {
	super();
    }

    /**
     * 
     */
    private static final long serialVersionUID=1L;

    public void create() throws BiffException, IOException, WriteException
    {
	writeWorkBook=Workbook.createWorkbook(new File("D:\\承保帳單"+date+".xls"));
	setSheet();
	getTotalbyte();
	type=0;
	row=1;
	settitle(writeSheet[0],0,"0");
	for(ReinsuranceBillingEntity bill : bills)
	{
	    ReinsuranceBill=bill;
	    setProgressBar();
	    setBody();
	    if("SUB-TOTAL:".equals(bill.getWORK_CESSION_NO()))
	    {
		type=Integer.parseInt(bill.getWORK_TYPE());
		settitle(writeSheet[type],0,bill.getWORK_TYPE());
		row=1;
	    }
	}
	writeWorkBook.write();
	writeWorkBook.close();
	JOptionPane.showMessageDialog(null,"檔案完成");
	dispose();
    }

    private void setBody() throws RowsExceededException, WriteException
    {
	writeSheet[type].addCell(new Label(0,row,ReinsuranceBill.getWORK_CESSION_NO(),cellFormat1));
	writeSheet[type].addCell(new Label(1,row,ReinsuranceBill.getWORK_PLY_LOC(),cellFormat1));
	writeSheet[type].addCell(new Label(2,row,ReinsuranceBill.getWORK_RISK_NO(),cellFormat1));
	writeSheet[type].addCell(new Label(3,row,ReinsuranceBill.getWORK_FAC_INT(),cellFormat1));
	writeSheet[type].addCell(new Label(4,row,ReinsuranceBill.getWORK_LIMIT_QSRATE(),cellFormat1));
	writeSheet[type].addCell(new Label(5,row,ReinsuranceBill.getWORK_ACC_RETRATE(),cellFormat1));
	writeSheet[type].addCell(new Label(6,row,ReinsuranceBill.getWORK_RISK_AMT(),cellFormat1));
	writeSheet[type].addCell(new Label(7,row,ReinsuranceBill.getWORK_OUR_QSAMT(),cellFormat1));
	writeSheet[type].addCell(new Label(8,row,ReinsuranceBill.getWORK_RATE(),cellFormat1));
	writeSheet[type].addCell(new Label(9,row,ReinsuranceBill.getWORK_RISKPRM_REIN(),cellFormat1));
	writeSheet[type].addCell(new Label(10,row,ReinsuranceBill.getWORK_BEGIN_DAY(),cellFormat1));
	writeSheet[type].addCell(new Label(11,row,ReinsuranceBill.getWORK_END_DAY(),cellFormat1));
	writeSheet[type].addCell(new Label(12,row,ReinsuranceBill.getWORK_DAYS(),cellFormat1));
	writeSheet[type].addCell(new Label(13,row,ReinsuranceBill.getWORK_DAYM(),cellFormat1));
	writeSheet[type].addCell(new Label(14,row,ReinsuranceBill.getWORK_PREM(),cellFormat1));
	writeSheet[type].addCell(new Label(15,row,ReinsuranceBill.getWORK_ISSU_DAY(),cellFormat1));
	writeSheet[type].addCell(new Label(16,row,ReinsuranceBill.getWORK_TYPE(),cellFormat1));
	row=row+1;

    }

    public void settitle(WritableSheet sheet, int row, String type)
	    throws RowsExceededException, WriteException
    {
	sheet.addCell((new Label(0,row,"Cession NO",cellFormat1)));
	sheet.addCell((new Label(1,row,"保單地址序號",cellFormat1)));
	sheet.addCell((new Label(2,row,"同險號",cellFormat1)));
	sheet.addCell((new Label(6,row,"RISK AMT",cellFormat1)));
	sheet.addCell((new Label(9,row,"REINSURED",cellFormat1)));
	sheet.addCell((new Label(10,row,"起始日",cellFormat1)));
	sheet.addCell((new Label(11,row,"到期日",cellFormat1)));
	sheet.addCell((new Label(12,row,"分子",cellFormat1)));
	sheet.addCell((new Label(13,row,"分母",cellFormat1)));
	sheet.addCell((new Label(14,row,"保費",cellFormat1)));
	sheet.addCell((new Label(15,row,"產生日期",cellFormat1)));
	sheet.addCell((new Label(16,row,"險種",cellFormat1)));
	if(type.equals("2")||type.equals("3"))
	{
	    sheet.addCell((new Label(3,row,"臨分比例",cellFormat1)));
	    sheet.addCell((new Label(4,row,"QS比例",cellFormat1)));
	    sheet.addCell((new Label(5,row,"自留額比例",cellFormat1)));
	    sheet.addCell((new Label(7,row,"QS保額",cellFormat1)));
	    sheet.addCell((new Label(8,row,"",cellFormat1)));
	}
	else
	{
	    sheet.addCell((new Label(3,row,"",cellFormat1)));
	    sheet.addCell((new Label(4,row,"限額代號",cellFormat1)));
	    sheet.addCell((new Label(5,row,"RISK ACCUMULATION",cellFormat1)));
	    sheet.addCell((new Label(7,row,"自留額",cellFormat1)));
	    sheet.addCell((new Label(8,row,"費率",cellFormat1)));
	}
    }

    @Override
    public void run()
    {
	try
	{
	    create();
	}
	catch (Exception e)
	{
	    new ErrorPage(e);
	}
    }

}
